import { Request, Response, NextFunction } from 'express';
import { verifyToken, JwtPayload } from '../utils/jwt';
declare global {
  namespace Express {
    interface Request {
      user?: JwtPayload;
    }
  }
}
export const authMiddleware = (req: Request, res: Response, next: NextFunction) => {
  const token = req.header('Authorization')?.replace('Bearer ', '');

  if (!token) {
    return res.status(200).json({ code: 401, message: '访问被拒绝，未提供令牌' });
  }

  const decoded = verifyToken(token);
  if (!decoded) {
    return res.status(200).json({ code: 402, message: '无效的令牌' });
  }

  // 将解码后的用户信息添加到请求对象中
  req.user = decoded;
  next();
};
// UPDATE users SET password='$2b$10$lzFbbvTGyyN7YE4WR4aP/eQZ9d6OOoWUGLmkqs5uzwv6n6oPIyAcq'  WHERE username='yuanyuehui袁越辉';
// select * from users where username='yuanyuehui袁越辉' order by user_index asc;